package com.lungle.warmdiary.app.view.me.fragment;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.Html;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;

import com.lungle.warmdiary.R;
import com.lungle.warmdiary.app.AppContext;
import com.lungle.warmdiary.app.api.remote.ApiFactory;
import com.lungle.warmdiary.app.base.BaseFragment;
import com.lungle.warmdiary.app.base.BaseSubscriber;
import com.lungle.warmdiary.app.model.Result;
import com.lungle.warmdiary.app.model.response.MediaAccount;
import com.lungle.warmdiary.app.utils.UIHelper;
import com.lungle.warmdiary.app.view.me.adapter.MediaCenterAdapter;
import com.umeng.analytics.MobclickAgent;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;


public class MediaCenterFragment extends BaseFragment {
    private static final String MEDIA_CENTER_SCREEN = "media center screen";

    private final int MEDIA_ACCOUNT_MAX = 5;

    @Bind(R.id.rv_accomnt_container)
    ListView mRvAccomntContainer;

    @Bind(R.id.tv_account_limit)
    TextView mTvAccountLimit;

    @Bind(R.id.btn_add_media_account)
    Button mBtnAddMediaAccount;

    private MediaCenterAdapter mAdapter;
    private List<MediaAccount> accounts = new ArrayList<>();


    @Override
    public View onCreateView(LayoutInflater inflater,
                             @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(getLayoutId(), container, false);
        initViews(view);
        return view;
    }


    @Override
    protected int getLayoutId() {
        return R.layout.fragment_bind_center;
    }

    private void initViews(View view) {
        ButterKnife.bind(this, view);
        mTvAccountLimit.setText(Html.fromHtml("最多设置<font color=#F94849>" + MEDIA_ACCOUNT_MAX + "</font>个帐号"));
        mRvAccomntContainer.setOnItemClickListener((parent, view1, position, id) -> {
            MediaAccount o = accounts.get(position);
            UIHelper.showMediaAddResult(getActivity(), o, MediaAddResultFragment.ACTION_UPDATE);
//            UIHelper.showMediaAccountDetail(getActivity(), o.getId());
        });
    }

    @Override
    public void onResume() {
        super.onResume();
        initData();
        MobclickAgent.onPageStart(MEDIA_CENTER_SCREEN);
        MobclickAgent.onResume(getActivity());

    }

    @Override
    public void onPause() {
        super.onPause();
        MobclickAgent.onPageEnd(MEDIA_CENTER_SCREEN);
        MobclickAgent.onPause(getActivity());

    }



    @Override
    public void initData() {
        getAccountList();
    }


    // TODO: 2016/8/14
    private void getAccountList() {
        showWaitDialog();
        int paltform = 1;
        //// TODO: 2016/8/19  
        ApiFactory.getUserApi().getMediaAccountList(1).subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new BaseSubscriber<Result<List<MediaAccount>>>(getActivity()) {
                    @Override
                    public void onCompleted() {
                        super.onCompleted();
                        hideWaitDialog();
                    }

                    @Override
                    public void onError(Throwable e) {
                        super.onError(e);
                        hideWaitDialog();
                    }

                    @Override
                    public void onNext(Result<List<MediaAccount>> mediaAccountListResult) {
                        super.onNext(mediaAccountListResult);
                        List<MediaAccount> data = mediaAccountListResult.getData();
                        if (!data.isEmpty()) {
                            AppContext.setMeidaAccount(true);
                        }else{
                            AppContext.setMeidaAccount(false);

                        }
                        accounts.clear();
                        accounts.addAll(data);
                        if (mAdapter == null) {
                            mAdapter = new MediaCenterAdapter(getActivity(), accounts);
                            mRvAccomntContainer.setAdapter(mAdapter);
                        }

                        mAdapter.notifyDataSetChanged();
                    }
                });
    }


    private boolean prepareAdd() {
        if (accounts.size() >= MEDIA_ACCOUNT_MAX) {
            return false;
        }
        return true;
    }


    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.unbind(this);
    }

    @OnClick({R.id.btn_add_media_account})
    public void onClick(View view) {
        switch (view.getId()) {

            case R.id.btn_add_media_account:
                if (!prepareAdd()) {
                    AppContext.showToast("帐号数量已达上线");
                    return;
                }
                UIHelper.showMeidaAdd(getActivity());
//                UIHelper.showMediaAddResult(getActivity(), 1, null);
                break;
        }
    }
}
